package edu.eas.admin.controller;

import edu.eas.admin.service.MenuService;
import edu.eas.common.api.CommonResult;
import edu.eas.common.dto.MenuNode;
import edu.eas.common.pojo.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cql
 */
@RestController
@Api(tags = "MenuController", description = "菜单管理")
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @ApiOperation("添加后台菜单")
    @PostMapping
    @ResponseBody
    public CommonResult create(@RequestBody Menu menu) {
        int count = menuService.create(menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("更新后台菜单")
    @PutMapping(value = "/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody Menu menu) {
        int count = menuService.update(id, menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("根据菜单编号获取详细信息")
    @GetMapping("{menuId}")
    public CommonResult<Menu> getMenu(@PathVariable Long menuId) {
        Menu menu = menuService.getMenu(menuId);
        return CommonResult.success(menu);
    }

    @ApiOperation("加载对应角色菜单列表树")
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public CommonResult<Map<String, Object>> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        Map<String, Object> map = new HashMap<>();
        // 1.根据角色ID查询菜单树信息
        List<Long> checkedKeys = menuService.selectMenuListByRoleId(roleId);
        // 2.构建前端所需要下拉树结构
        List<MenuNode> menus = menuService.selectMenuList();
        map.put("checkedKeys", checkedKeys);
        map.put("menus", menus);
        return CommonResult.success(map);
    }


    @ApiOperation("获取菜单下拉树列表(只能获取当前用户可以看到的菜单)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名称,模糊匹配", required = false),
            @ApiImplicitParam(name = "status", value = "菜单状态(0:不可用;1:可用)", required = false)
    })
    @GetMapping(value = "/treeselect")
    public CommonResult<List<MenuNode>> treeselect(@RequestParam(value = "menuName", defaultValue = "") String menuName,
                                                   @RequestParam(value = "status", defaultValue = "-1") String status) {
        List<MenuNode> list = menuService.treeselect(menuName, Integer.parseInt(status));
        return CommonResult.success(list);
    }


    @ApiOperation("树形结构返回菜单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuName", value = "菜单名称,模糊匹配", required = false),
            @ApiImplicitParam(name = "status", value = "菜单状态(0:不可用;1:可用)", required = false)
    })
    @GetMapping(value = "/treeList")
    public CommonResult<List<MenuNode>> treeList(@RequestParam(value = "menuName", defaultValue = "") String menuName,
                                                 @RequestParam(value = "status", defaultValue = "-1") String status) {
        List<MenuNode> list = menuService.treeList(menuName, Integer.parseInt(status));
        return CommonResult.success(list);
    }

    @ApiOperation("根据ID删除后台菜单(逻辑删除)")
    @DeleteMapping(value = "/{id}")
    public CommonResult delete(@PathVariable("id") Long id) {
        int count = menuService.deleteById(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed("存在子菜单,删除失败");
        }
    }


}
